package nowCoder;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class HJ68 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        String[] name = new String[n];
        int[] scone = new int[n];
        Integer[] ss = new Integer[n];
        for (int i = 0; i < n; i++) {
            name[i] = sc.next();
            scone[i] = sc.nextInt();
            ss[i] = scone[i];
        }
        if (m == 0)
            Arrays.sort(ss);
        else
            Arrays.sort(ss,Comparator.reverseOrder());
        for (int i = 0; i < n; i++) {
            int c = ss[i];
            for (int j = 0; j < n; j++) {
                if (c == scone[j]){
                    scone[j] = -1;
                    System.out.println(name[j] +" "+ c);
                    break;
                }
            }
        }
    }
}
